﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

namespace ClockSync2
{
    public static class utils
    {
        public static IPAddress FindLanAddress()
        {
            IPAddress gateway = FindGetGatewayAddress();
            if (gateway == null)
                return null;

            IPAddress[] pIPAddress = Dns.GetHostAddresses(Dns.GetHostName());

            foreach (IPAddress address in pIPAddress)
            {
                if (IsAddressOfGateway(address, gateway))
                    return address;
            }
            return null;
        }

        static bool IsAddressOfGateway(IPAddress address, IPAddress gateway)
        {
            if (address != null && gateway != null)
                return IsAddressOfGateway(address.GetAddressBytes(), gateway.GetAddressBytes());
            return false;
        }

        static bool IsAddressOfGateway(byte[] address, byte[] gateway)
        {
            if (address != null && gateway != null)
            {
                int gwLen = gateway.Length;
                if (gwLen > 0)
                {
                    if (address.Length == gateway.Length)
                    {
                        --gwLen;
                        int counter = 0;
                        for (int i = 0; i < gwLen; i++)
                        {
                            if (address[i] == gateway[i])
                                ++counter;
                        }
                        return (counter == gwLen);
                    }
                }
            }
            return false;
        }

        static IPAddress FindGetGatewayAddress()
        {
            IPGlobalProperties ipGlobProps = IPGlobalProperties.GetIPGlobalProperties();

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                IPInterfaceProperties ipInfProps = ni.GetIPProperties();
                foreach (GatewayIPAddressInformation gi in ipInfProps.GatewayAddresses)
                    return gi.Address;
            }
            return null;
        }

        public static byte[] StrToByteArray(string str)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetBytes(str);
        }

        public static string ByteToStrArray(byte[] word)
        {
            string ret = "";
            for (int i = 0; i < word.Length; i++)
                ret+=Convert.ToChar(word[i]);
            if(ret.Contains('\0'))
                ret = ret.Substring(0, ret.IndexOf('\0'));

            return ret;
        }

        public static string DateTimeAsString(DateTime CurrentTime)
        {
            string t = CurrentTime.Hour + ":" + CurrentTime.Minute + ":" + CurrentTime.Second + ":" + CurrentTime.Millisecond;
            return t;
        }

        public static long subDateTimesGetTotMS(DateTime a, DateTime b)
        {
            string aa = utils.DateTimeAsString(a);
            string bb = utils.DateTimeAsString(b);
            
            string[] aaa = aa.Split(':');
            string[] bbb = bb.Split(':');

            long aaaa = utils.getTimeInMS(aaa);
            long bbbb = utils.getTimeInMS(bbb);
            
            return aaaa - bbbb;
        }

        public static DateTime StringAsDateTime(String time,DateTime CurrentTime)
        {
            string[] t = time.Split(':');
            DateTime temp = new DateTime(CurrentTime.Year, CurrentTime.Month, CurrentTime.Day, int.Parse(t[0]), int.Parse(t[1]), int.Parse(t[2]), int.Parse(t[3]));
            return temp;
        }

        public static long getTimeInMS(string[] hisTime)
        {
            long hisMSTime = 3600000 * int.Parse(hisTime[0]) + 60000 * int.Parse(hisTime[1]) + 1000 * int.Parse(hisTime[2]) + 1 * int.Parse(hisTime[3]);
            return hisMSTime;
        }

        public static string TimeInMSAsString(long time)
        {
            long tempTime=time;
            
            long h = tempTime / 3600000;
            tempTime -= h * 3600000;

            long m = tempTime / 60000;
            tempTime -= m * 60000;

            long s = tempTime / 1000;
            tempTime -= s * 1000;

            long ms = tempTime;

            return h + ":" + m + ":" + s + ":" + ms;

        }
    }



}
